package com.zhongshan.ga.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 
 * Title :
 * 
 * Description :
 * 
 * CreatedTime : 2015-2-10
 * 
 * @author wuliao & kakaxi & yi & hui & Charl_ZH
 * 
 * @version 1.0
 * 
 */
public class DateTimeConvert {

	//
	private final static int DIFFER_SECOND = 1;
	private final static int DIFFER_MIMUTE = 60 * DIFFER_SECOND;
	private final static int DIFFER_HOUR = 60 * DIFFER_MIMUTE;
	private final static int DIFFER_DAY = 24 * DIFFER_HOUR;

	//
	private final static String DIFFER_SECOND_STR = "秒前";
	private final static String DIFFER_MIMUTE_STR = "分钟前";
	private final static String DIFFER_HOUR_STR = "小时前";
	private final static String DIFFER_DAY_STR = "天前";

	/**
	 * 
	 * @param dateTimeStr
	 * @param differDay
	 * @return
	 */
	public static String toDifferDay(String dateTimeStr, int differDay) {
		SimpleDateFormat differDateFormat = (SimpleDateFormat) SimpleDateFormat
				.getTimeInstance();
		differDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		long dateTimeNum = 0;
		try {
			date = differDateFormat.parse(dateTimeStr);
			dateTimeNum = date.getTime();
			long differSecond = (System.currentTimeMillis() - dateTimeNum) / 1000;

			long differNum = 0;
			System.out.println("dateTime : cu--" + System.currentTimeMillis()
					+ " time--" + dateTimeNum + " diffs--" + differSecond
					+ " dateStr--" + dateTimeStr + " covertTime--"
					+ date.toString());
			if (differSecond < DIFFER_MIMUTE) {
				// 秒
				return differSecond + DIFFER_SECOND_STR;
			} else if (differSecond < DIFFER_HOUR) {
				// 分
				differNum = differSecond / DIFFER_MIMUTE;
				return differNum + DIFFER_MIMUTE_STR;
			} else if (differSecond < DIFFER_DAY) {
				// 小时
				differNum = differSecond / DIFFER_HOUR;
				return differNum + DIFFER_HOUR_STR;
			} else if (differSecond < DIFFER_DAY * (differDay + 1)) {
				// 天
				differNum = differSecond / DIFFER_DAY;
				return differNum + DIFFER_DAY_STR;
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar calendar = Calendar.getInstance(Locale.getDefault());
		int currentYear = calendar.get(Calendar.YEAR);
		calendar.setTimeInMillis(dateTimeNum);
		int year = calendar.get(Calendar.YEAR);
		if (currentYear == year) {
			differDateFormat.applyPattern("MM-dd");
		} else {
			differDateFormat.applyPattern("yyyy-MM-dd");
		}
		return differDateFormat.format(calendar.getTimeInMillis());
	}
}
